<script>
// import useLocale from '@/languages/useLocale';
import { ElMessageBox, ElMessage } from "element-plus";
// 定义一些公共的属性和方法
const httpUrl = "http://127.0.0.1:8080/";
let vscode = undefined;
let isWebTest = false;
try {
  console.log("call acquireVsCodeApi.");
  vscode = acquireVsCodeApi();
} catch (e) {
  console.log("catch error acquireVsCodeApi.");
  console.log(e);
  isWebTest = true;
}
function commonFun() {
  console.log("公共方法");
}
const global = {
  httpUrl,
  vscode,
  commonFun,
};

/**
 * @description: acquireVsCodeApi说明
 * webview和普通网页一样，并不能直接调用任何VSCode API。
 * 它唯一特别之处就在于多了一个名叫acquireVsCodeApi的方法
 * 执行这个方法会返回一个简易版的vscode对象，具有如下三个方法：
 *    getState()
 *    postMessage(msg)
 *    setState(newState)
 * 这样的话，我们可以发消息让extension去帮我们发送http请求！
 * @param {*}
 * @return {*}
 * @author: kylinos.cn
 */
/**
 * 判断当前是否有vscode环境
 * @param {发送的消息} message
 */
const isNoVscode = () => {
  return global.vscode === undefined;
};
/**
 * 向vscode发送消息
 * @param {发送的消息} message
 */
const postMessage = (message) => {
  console.log(global.vscode);
  console.log(message);
  if (global.vscode) {
    global.vscode.postMessage(message);
  }
};
// const callChooseDir = (projectOption) => {
//   console.log("function getDir");
//   postMessage({
//     type: "CHOOSE_DIR",
//     projectOption: projectOption,
//   });
// };
/**
 * @description: 请求vscode获取当前默认路径
 * @param {*} model
 * @return {*}
 * @author: kylinos.cn
 */
const getDir = (model) => {
  console.log("function getDir");
  console.log(model);
  postMessage({
    type: "CHOOSE_DIR",
    model: model,
  });
};
const showSuccess = (msg) => {
  ElMessage({
    showClose: true,
    message: msg,
    type: 'success',
  })
};
const showWarn = (msg) => {
  ElMessage({
    showClose: true,
    message: msg,
    type: 'warning',
  })
};
const showError = (msg) => {
  ElMessage({
    showClose: true,
    message: msg,
    type: 'error',
  })
};
/**
 * @description: 请求vscode获取用户home路径
 * @param {*}
 * @return {*}
 * @author: kylinos.cn
 */
const getHomeDir = () => {
  console.log("function getHomeDir");
  postMessage({
    type: "GET_HOME_DIR",
  });
};
const checkUserConfig = (message) => {
  console.log("function checkUserConfig");
  postMessage(message);
};
const checkData = (message) => {
  console.log("function checkData");
  postMessage(message);
};
const createProject = (message) => {
  console.log("function createProject");
  console.log(message)
  postMessage(message);
};
/**
 * @description: 请求vscode设置当前默认路径
 * @param {*} projectOption
 * @return {*}
 * @author: kylinos.cn
 */
const setDir = (projectOption) => {
  console.log("function setDir: ");
  postMessage({
    type: "SET_DIR",
    data: projectOption,
  });
};
/**
 * @description: 用户点击保留默认地址选项时，请求vscode记录
 * @param {*} event
 * @param {*} projectOption
 * @return {*}
 * @author: kylinos.cn
 */
const onRemberDirChanged = (event, projectOption) => {
  console.log(" function onRemberDirChanged :");
  projectOption.isSaveDir = event.target.checked;
  setDir(projectOption);
};
/**
 * @description: 请求vscode发送配置信息
 * @param {*}
 * @return {*}
 * @author: kylinos.cn
 */
const showConfig = () => {
  postMessage({
    type: "SHOW_CONFIG",
  });
};
const callCommand = (projectType, command, param) => {
    postMessage({
        type: 'CALL_COMMAND',
        projectType: projectType,
        command: command,
        param: param
    });
}
/**
 * @description: 提交配置信息变更
 * @param {*} projectOption
 * @return {*}
 * @author: kylinos.cn
 */
const saveConfig = (projectOption) => {
  console.log("call saveConfig");
  postMessage({
    type: "MODIFY_CONFIG",
    data: projectOption,
  });
};
/**
 * @description: 放弃修改
 * @param {*}
 * @return {*}
 * @author: kylinos.cn
 */
const closeConfig = () => {
  postMessage({
    type: "CANCEL_CONFIG",
  });
};
console.log(showConfig);
  // 请求vscode提交配置修改
  console.log(saveConfig);
  // 请求vscode取消配置
  console.log(closeConfig);
  // 请求vscode获取当前默认路径
  console.log(getDir);
  // 请求vscode设置当前默认路径
  console.log(setDir);
  // 是否点击保存默认路径
  console.log(onRemberDirChanged);
  // 获取用户目录
  console.log(getHomeDir);
  console.log(checkUserConfig);
  console.log(createProject);
  // 发送消息到vscode
  console.log(postMessage);
  console.log(global);
  console.log("===")
export {
  // 请求vscode读取配置
  showConfig,
  // 请求vscode提交配置修改
  saveConfig,
  // 请求vscode取消配置
  closeConfig,
  // 请求vscode获取当前默认路径
  getDir,
  // 请求vscode设置当前默认路径
  setDir,
  // 是否点击保存默认路径
  onRemberDirChanged,
  // 获取用户目录
  getHomeDir,
  checkUserConfig,
  checkData,
  createProject,
  // 统一监听实现
  // commonListener,
  isNoVscode,
  // 发送消息到vscode
  postMessage,
  global,
  showSuccess,
  showWarn,
  showError,
  isWebTest,
  callCommand
};
export default{

}
</script>
